package manager;

import java.io.IOException;
import java.net.MalformedURLException;

import callers.esi.ESIServiceCaller;
import callers.plm.PLMServiceCaller;
import analyzers.InputAnalyzer;
import structures.ServiceCallInfo;
import structures.SessionVariables;
import xml.sax.esi.EsiXMLProcessor;
import xml.sax.plm.PlmInXMLProcessor;
import exception.IBServiceCallException;
import exception.IBSessionTimeOutException;
import exception.IBToolsException;

public class ServiceCallManager {
	
	private boolean simpleCall;

//    public void simpleCall(SessionVariables variables) throws MalformedURLException, IOException, Exception{
//    	simpleCall = true;
//    	call(variables);
//    }
//    public void detailCall(SessionVariables variables) throws MalformedURLException, IOException, Exception{
//    	simpleCall = false;
//    	call(variables);
//    }
//	public static void call(SessionVariables variables) throws IBToolsException, IBServiceCallException{
//    	
//    	ServiceCallInfo callInfo = new ServiceCallInfo();
//    	
//    	int maxTry = 20;
//    	int tryUntilCount = 1;
//    	int errorCount = 0;
//    	boolean successful = false;
//    	if(variables.tryUntil != null && variables.tryUntil < maxTry)
//    		tryUntilCount = variables.tryUntil;
//    	else
//    		tryUntilCount = maxTry;
//
//    	while(errorCount < tryUntilCount){
//    		try {
//    			InputAnalyzer.simpleXmlAnalysis(variables);
//    			
//    	    	if(variables.selectedInputTab.equals("Table")){
//    	        	setXml(true);
//    	        	callInfo.setServiceName(variables.esiServiceParameterTable.getServiceName());
//    	    	}
//    	    	if(variables.xmlStringType.equals("RC"))
//    	    		callInfo = callXMLFromPLM(callInfo);
//    	    	else
//    	    		callInfo = callXMLFromESI(callInfo);
//    	    	successful = true;
//			} catch (IBToolsException e) {
//				successful = false;
//				errorCount++;
//				addError(e);
//				if(errorCount == tryUntil)
//					if(errorCount==1)
//						throw e;
//			}
//    		if(successful)
//    			break;
//    	}
//		try {
//	    	LogManager.logServiceCallSuccess(callInfo);
//		} catch (IBSessionTimeOutException e) {
//			e.printStackTrace();
//		}
//    }
//    public ServiceCallInfo callXMLFromPLM(SessionVariables variables, ServiceCallInfo callInfo) throws IBToolsException, IBServiceCallException{
//    	
//    	if(variables.xmlString != null && !variables.xmlString.equals("")){
//    		callInfo = new ServiceCallInfo();
//    		callInfo.setServiceName(PlmInXMLProcessor.getXmlServiceName(variables.xmlString));
//    		callInfo.setInXml(variables.xmlString);
//    		callInfo.setLocal(variables.localCall);
//    		callInfo.setTargetIp(variables.ip);
//        	PLMServiceCaller caller = new PLMServiceCaller();
//        	if(simpleCall){
//        		callInfo = caller.callSimple(callInfo, environment);
//        	}
//        	else{
//        		callInfo.setTargetServer(server);
//        		callInfo = caller.callDetail(callInfo);
//        	}
//    		outXml = callInfo.getOutXml();
//    	}
//    	else{
//    		outXml = "";
//    	}
//    	responsePlmXmlToOutTable(callInfo);
//    	return callInfo;
//    }
//    
//    public ServiceCallInfo callXMLFromESI(ServiceCallInfo callInfo) throws IBToolsException, IBServiceCallException{
//
//    	if(variables.xmlString != null && !variables.xmlString.equals("")){
//    		callInfo = new ServiceCallInfo();
//    		callInfo.setServiceName(EsiXMLProcessor.getXmlServiceName(variables.xmlString));
//    		callInfo.setInXml(variables.xmlString);
//    		callInfo.setLocal(localCall);
//    		callInfo.setTargetIp(ip);
//    		callInfo.setPort(port);
//        	TCPServiceCaller caller = new TCPServiceCaller();
//        	if(simpleCall){
//        		callInfo = caller.callSimple(callInfo, environment);
//        	}
//        	else{
//        		callInfo.setTargetServer(server);
//        		callInfo = caller.callDetail(callInfo);
//        	}
//    		outXml = callInfo.getOutXml();
//    	}
//    	else{
//    		outXml = "";
//    	}
//    	responseEsiXmlToOutTable(callInfo);
//    	return callInfo;
//    }
}
